package cn.wolfcode.crm.service.impl;

import cn.wolfcode.crm.domain.Employee;
import cn.wolfcode.crm.domain.PotentialStudent;
import cn.wolfcode.crm.mapper.PotentialStudentMapper;
import cn.wolfcode.crm.query.PotentialStudentQueryObject;
import cn.wolfcode.crm.query.QueryObject;
import cn.wolfcode.crm.service.IPotentialStudentService;
import cn.wolfcode.crm.util.PageResult;
import cn.wolfcode.crm.util.UserContext;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class PotentialStudentServiceImpl implements IPotentialStudentService {
    @Autowired
    private PotentialStudentMapper potentialStudentMapper;
    @Override
    public int saveOrUpdate(PotentialStudent record) {
        record.setInputUser(UserContext.getUserContext());
        record.setSalesman(UserContext.getUserContext());
        if (record.getId() == null) {
            record.setPutPoolState(false);
            return potentialStudentMapper.insert(record);
        } else {
            return potentialStudentMapper.updateByPrimaryKey(record);
        }
    }

    @Override
    public PotentialStudent selectByPrimaryKey(Long id) {
        if (id == null) {
            return null;
        }
        return potentialStudentMapper.selectByPrimaryKey(id);
    }

    @Override
    public PageResult query(PotentialStudentQueryObject qo) {
        Employee employee = UserContext.getUserContext();

        //管理员能查看所有
        if (!employee.getAdmin()) {
            //不是管理员,设置empId
            qo.setEmpId(employee.getId());
        }

        int count = potentialStudentMapper.queryCount(qo);
        if (count == 0) {
            return new PageResult(0, Collections.EMPTY_LIST);
        }
        List<PotentialStudent> data = potentialStudentMapper.queryForList(qo);
        return new PageResult(count, data);
    }

    @Override
    public List<PotentialStudent> queryIntroducerStudent(String state) {
        return potentialStudentMapper.queryIntroducerStudent(state);
    }

    @Override
    public List<PotentialStudent> all() {
        return potentialStudentMapper.selectAll();
    }

    @Override
    public void update(PotentialStudent student) {
        potentialStudentMapper.update(student);
    }



    @Override
    public boolean validateTel(Long id, String tel) {
        PotentialStudent potentialStudent = potentialStudentMapper.validateTel(id, tel);
        return potentialStudent == null || potentialStudent.getId().equals(id);
    }

    @Override
    public boolean validateQq(Long id, String qq) {
        PotentialStudent potentialStudent = potentialStudentMapper.validateQq(id,qq);
        return potentialStudent == null || potentialStudent.getId().equals(id);
    }

    @Override
    public void updateTracking(Long id) {
        if (id != null) {
            potentialStudentMapper.updateTracking(new Date(),id);
        }
    }

    //转正操作
    @Override
    public void updateForEnroll(PotentialStudent ps,String[] employmentIntention) {
        ObjectMapper mapper = new ObjectMapper();
        try {
            String empIntention = mapper.writeValueAsString(employmentIntention);
            ps.setEmploymentIntention(empIntention);
            potentialStudentMapper.update(ps);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //更改资源池的状态
    @Override
    public void putPoolState(Long id) {
        try{
            potentialStudentMapper.putPoolState(id);
        }catch (Exception e){
            e.printStackTrace();
            throw new RuntimeException("操作失败");
        }
    }

    @Override
    public List<PotentialStudent> selectByPutPoolState(QueryObject qo) {
        return potentialStudentMapper.selectByPutPoolState(qo);
    }
}
